package test;

import java.util.*;

public class SearchMaxCallTime {
    Set<String> start = new HashSet<>();
    Set<String> remove = new HashSet<>();
    List<List<String>> datas = null;

    public int searchMaxCallTime(List<List<String>> datas) {
        this.datas = datas;
        for (List<String> data : datas) {
            start.add(data.get(0));
            remove.add(data.get(1));
        }
        start.removeAll(remove); // 找到起点

        int max = 0;
        for (String s : start) {
            int m = this.maxDistinceFromThisNodeToEnd(s);
            max = m > max ? m : max;
        }
        return max;
    }

    /**
     * 计算当前这个节点到最后一个节点的最大距离
     *
     * @param thisNode
     * @return
     */
    private int maxDistinceFromThisNodeToEnd(String thisNode) {
        int max = 0;
        for (List<String> data : datas) {
            if (data.get(0).equals(thisNode)) {
                int thisRootMax = Integer.valueOf(data.get(2)) + maxDistinceFromThisNodeToEnd(data.get(1)); // 这里有重复计算
                max = thisRootMax > max ? thisRootMax : max;
            }
        }
        return max;
    }

    public static void main(String[] args) {
        SearchMaxCallTime s = new SearchMaxCallTime();
        List<List<String>> datas = new ArrayList<>();
        List<String> data0 = Arrays.asList("A", "B", "100");
        List<String> data1 = Arrays.asList("A", "C", "200");
        List<String> data2 = Arrays.asList("A", "F", "100");
        List<String> data3 = Arrays.asList("B", "D", "100");
        List<String> data4 = Arrays.asList("D", "E", "50");
        List<String> data5 = Arrays.asList("C", "G", "300");
        datas.add(data0);
        datas.add(data1);
        datas.add(data2);
        datas.add(data3);
        datas.add(data4);
        datas.add(data5);
        System.out.println(s.searchMaxCallTime(datas));
    }
}
